package org.kernely.ui.settings;

import org.kernely.ui.mvp.IKernelyView;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

/**
 *	The view of the settings of the user.
 */
public class KernelySettingsView extends GridLayout implements IKernelyView {
	
	private static final long serialVersionUID = 1056106551489049821L;
	private TextField oldPasswordField;
	private TextField newPasswordField;
	private TextField confirmPasswordField;
	private Button savePasswordButton;
	private NativeSelect homePagesBox;
	private Button applyButton;
	private Button resetButton;
	
	/**
	 * KernelySettingsView constructor.
	 */
	public KernelySettingsView(){
		Label title = new Label("Settings");
		title.setStyleName(Reindeer.LABEL_H1);
		title.setSizeUndefined(); // Usefull to center the label.

		// Password components
		
		Panel passwordsPanel = new Panel();
		passwordsPanel.setSizeUndefined();
		Label passwordsTitle = new Label("Password");
		passwordsTitle.setStyleName(Reindeer.LABEL_H2);
		
		oldPasswordField = new TextField("Password");
		newPasswordField = new TextField("New password");
		confirmPasswordField = new TextField("Confirm password");
		oldPasswordField.setSecret(true);
		newPasswordField.setSecret(true);
		confirmPasswordField.setSecret(true);
		oldPasswordField.setWidth(200,UNITS_PIXELS);
		newPasswordField.setWidth(200,UNITS_PIXELS);
		confirmPasswordField.setWidth(200,UNITS_PIXELS);
		savePasswordButton = new Button("Save");		
		oldPasswordField.setRequired(true);
		newPasswordField.setRequired(true);
		confirmPasswordField.setRequired(true);
		oldPasswordField.setRequiredError("You must enter your old password.");
		newPasswordField.setRequiredError("You must enter a new password.");
		confirmPasswordField.setRequiredError("You must fill the \"Confirm password\" field.");
		
		VerticalLayout passwordsLayout = new VerticalLayout();
		passwordsPanel.setContent(passwordsLayout);
		passwordsPanel.addComponent(passwordsTitle);
		passwordsPanel.addComponent(oldPasswordField);
		passwordsPanel.addComponent(newPasswordField);
		passwordsPanel.addComponent(confirmPasswordField);
		passwordsPanel.addComponent(savePasswordButton);
		passwordsLayout.setComponentAlignment(savePasswordButton,Alignment.MIDDLE_RIGHT);
		passwordsLayout.setMargin(true,true,true,true);
		passwordsLayout.setSpacing(true);
		passwordsTitle = new Label("Password");
		passwordsTitle.setStyleName(Reindeer.LABEL_H2);

		// Home page components
		Panel homePagePanel = new Panel();
		VerticalLayout homePageLayout = new VerticalLayout();
		homePageLayout.setMargin(true,true,true,true);
		homePageLayout.setSpacing(true);
		homePagePanel.setContent(homePageLayout);
		homePagePanel.setSizeUndefined();
		
		Label homePageTitle = new Label("Home page");
		homePageTitle.setStyleName(Reindeer.LABEL_H2);
		
		homePagesBox = new NativeSelect();
		homePagesBox.setNullSelectionAllowed(false);
		applyButton = new Button("Apply");
		resetButton = new Button("Reset");
		HorizontalLayout homeButtonsLayout = new HorizontalLayout();
		homeButtonsLayout.addComponent(resetButton);
		homeButtonsLayout.addComponent(applyButton);
		homeButtonsLayout.setComponentAlignment(resetButton,Alignment.MIDDLE_RIGHT);
		homeButtonsLayout.setComponentAlignment(applyButton,Alignment.MIDDLE_RIGHT);
		
		homePagePanel.addComponent(homePageTitle);
		homePagePanel.addComponent(new Label("You can choose the default home page here."));
		homePagePanel.addComponent(homePagesBox);
		homePagePanel.addComponent(homeButtonsLayout);
		homePageLayout.setComponentAlignment(homeButtonsLayout,Alignment.MIDDLE_RIGHT);

		
		
		this.setColumns(4);
		this.setRows(2);
		this.addComponent(title,0,0,3,0);
		this.setComponentAlignment(title,Alignment.MIDDLE_CENTER);
		this.addComponent(passwordsPanel,0,1);
		this.addComponent(homePagePanel,1,1);
		this.setSizeFull();
	}
	
	
	
	
	/**
	 * Gets the oldPasswordField
	 * @return the oldPasswordField
	 */
	public final TextField getOldPasswordField() {
		return oldPasswordField;
	}




	/**
	 * Gets the newPasswordField
	 * @return the newPasswordField
	 */
	public final TextField getNewPasswordField() {
		return newPasswordField;
	}




	/**
	 * Gets the confirmPasswordField
	 * @return the confirmPasswordField
	 */
	public final TextField getConfirmPasswordField() {
		return confirmPasswordField;
	}




	/**
	 * Gets the savePasswordButton
	 * @return the savePasswordButton
	 */
	public final Button getSavePasswordButton() {
		return savePasswordButton;
	}




	/**
	 * This method return the component. (Useless because we use the MVP pattern, see the wiki).
	 */
	@Override
	public final Component getComponent() {
		return this;
	}




	/**
	 * Gets the homePagesBox
	 * @return the homePagesBox
	 */
	public NativeSelect getHomePagesBox() {
		return homePagesBox;
	}




	/**
	 * Gets the applyButton
	 * @return the applyButton
	 */
	public Button getApplyButton() {
		return applyButton;
	}




	/**
	 * Gets the resetButton
	 * @return the resetButton
	 */
	public Button getResetButton() {
		return resetButton;
	}

	
	
	
}
